/**
 * @Author: Ray丶X
 * @Date: 2020/3/24
 */

// 事件驱动程序
var events = require('events');

var eventEmitter = new events.EventEmitter();

var connectHandle = function connected() {
  console.log('连接成功');
  eventEmitter.emit('data_received');
};

eventEmitter.on('connection', connectHandle);

eventEmitter.on('data_received', function () {
  console.log('接收数据成功');
});

eventEmitter.emit('connection');
console.log('执行完成');

// 结果
// 连接成功
// 接收数据成功
// 执行完成


eventEmitter.on('some_event', function (arg1, arg2) {
  console.log('event1' , arg1, arg2);
});

eventEmitter.on('some_event', function (arg1, arg2) {
  console.log('event2', arg1, arg2);
});

eventEmitter.emit('some_event', 'arg11', 'arg22');

// 结果
// event1 arg11 arg22
// event2 arg11 arg22
